import axios from "axios"

let baseURL = process.env.NODE_ENV === 'development' ? 'http://localhost:9000/' : '/'
const server  = axios.create({
    baseURL,
    timeout: 10000
})

server.interceptors.request.use(config=>{
    config.headers['Content-Type'] = "application/x-www-form-urlencoded"
    config.headers['token'] = sessionStorage.getItem("cxl_Token")
    return config
})

server.interceptors.response.use(config=>{
    return config
})

export const get = (url:string,data:any)=>{
    return new Promise(async (resolve,reject)=>{
        const result = await server({
            method: "get",
            params: data,
            url
        })
        if(result){
            resolve(result.data)
        }else{
            reject(result)
        }
    })    
}
export const post = (url:string,data:any)=>{
    return new Promise(async (resolve,reject)=>{
        const result = await server({
            method: "post",
            data,
            url
        })
        if(result){
            resolve(result)
        }else{
            reject(result)
        }
    })    
}

export default server
 